var id = T.p("id");
var subjectId =T.p("subjectId");
var subjectList = T.getDictDynas("subject");
var isShowCond = T.p("isShowCond");
var workRefer= T.getDictItems("taskRefer");
var misfire= T.getDictItems("misfire");
var levels = T.getDictItems("work_level");
var vm = new Vue({
	el:'#rrapp',
	data:{
		title:"新增",
		work:{
		    referType : workRefer[0].dictCode,
			levelNum:levels[0].dictCode,
            status:1,
            subjectId:subjectId
		},
		works:[],
		referWorkId:[],
        workPrivateParams:[]
	},
	mounted:function(){

	},
	created: function() {
		if(id != null){
			this.title = "修改";
			this.getInfo(id)
		}
		this.getWorks();
        this.workPrivateParams = T.getDictDynas("work_private_params",{workId:99999999});
    },
	methods: {
	   changeReferType:function (event) {
                var selectValue = event.target.value;
                workRefer.forEach(function (item) {
                    if(selectValue == item.dictCode){
                        vm.workItem.referType = item.dictCode;
                    }
                });
            },
		getInfo: function(id){
			$.get("../work/info/"+id, function(r){
                vm.work = r.work;
                vm.workPrivateParams = T.getDictDynas("work_private_params",{workId: vm.work.id});
                if(r.work.referWorkId){
                    vm.referWorkId=r.work.referWorkId.split(",");
                }
            });
		},
		getWorks:function(){
		 $.get("../work/list", function(r){
                    	var arr = new Array();
                        for(var i in r.page.data){
                            arr.push({
        						value:r.page.data[i].id,
                                label:r.page.data[i].workName
        					})
                        }
        				vm.works = arr
                    });
		},
		saveOrUpdate: function (event) {
			var url = vm.work.id == null ? "../work/save" : "../work/update";
			vm.work.referWorkId = vm.referWorkId.join(",");
			$.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.work),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
                            location.href = "/work/work.html?subjectId=" + subjectId+"&isShowCond="+ isShowCond;
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		back: function (event) {
			history.go(-1);
		}
	}
});
